package com.fare.taluo.web.action.detect.sys;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.util.value.NumberUtils;
import com.fare.taluo.web.action.detect.ComputeInfoAction;
/**
 * JVM堆资源监控
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-11 下午1:51:22
 * @version v1.0
 */
@Namespace("/")
@Controller("jvm")
@Scope("prototype")
public class JvmAction extends ComputeInfoAction
{

	private static final long serialVersionUID = 0x76a3eaca2f552e8L;
	/**
	 * Java虚拟机中的内存总量(java虚拟机现在已经从操作系统那里挖过来的内存大小)
	 */
	private double totalMem;
	/**
	 * Java虚拟机使用的最大内存量(java虚拟机[进程]能够从操作系统那里挖到的最大的内存)
	 */
	private double maxMem;
	/**
	 * 已使用内存大小
	 */
	private double UsedMem;
	/**
	 * 未使用内存大小
	 */
	private double freeMem;
	/**
	 * 已使用内存占内存总量百分比
	 */
	private double percUsed;
	/**
	 * 未已使用内存占内存总量百分比
	 */
	private double percFree;

	public JvmAction()
	{
	}

	public String show() throws Exception
	{
		Runtime runtime = Runtime.getRuntime();
		freeMem = NumberUtils.div(runtime.freeMemory(), 1048576D, 2);
		UsedMem = NumberUtils.div(runtime.totalMemory() - runtime.freeMemory(), 1048576D, 2);
		totalMem = NumberUtils.div(runtime.totalMemory(), 1048576D, 2);
		maxMem = NumberUtils.div(runtime.maxMemory(), 1048576D, 2);
		returnPageURL = "/system/detect/jvm.jsp";
		return "dispatcher";
	}

	public double getTotalMem()
	{
		return totalMem;
	}

	public void setTotalMem(double totalMem)
	{
		this.totalMem = totalMem;
	}

	public double getMaxMem()
	{
		return maxMem;
	}

	public void setMaxMem(double maxMem)
	{
		this.maxMem = maxMem;
	}

	public double getUsedMem()
	{
		return UsedMem;
	}

	public void setUsedMem(double usedMem)
	{
		UsedMem = usedMem;
	}

	public double getFreeMem()
	{
		return freeMem;
	}

	public void setFreeMem(double freeMem)
	{
		this.freeMem = freeMem;
	}

	public double getPercUsed()
	{
		if (getTotalMem() != 0.0D)
			percUsed = getUsedMem() / getTotalMem();
		return percUsed;
	}

	public void setPercUsed(double percUsed)
	{
		this.percUsed = percUsed;
	}

	public double getPercFree()
	{
		if (getTotalMem() != 0.0D)
			percFree = getFreeMem() / getTotalMem();
		return percFree;
	}

	public void setPercFree(double percFree)
	{
		this.percFree = percFree;
	}
}
